package com.talkweb.osmharmony.views.overlay.infowindow;

import com.talkweb.osmharmony.ResourceTable;
import com.talkweb.osmharmony.api.IMapView;
import com.talkweb.osmharmony.util.Log;
import com.talkweb.osmharmony.views.MapView;
import com.talkweb.osmharmony.views.overlay.Marker;
import com.talkweb.osmharmony.views.overlay.OverlayWithIW;

import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;


/**
 * {@link com.talkweb.osmharmony.views.overlay.infowindow.BasicInfoWindow} is the default
 * implementation of {@link com.talkweb.osmharmony.views.overlay.infowindow.InfoWindow} for an
 * {@link com.talkweb.osmharmony.views.overlay.OverlayWithIW}.
 * <p>
 * It handles a title, a description and a sub-description.
 * Clicking on the bubble will close it.
 *
 * <img alt="Class diagram around Marker class" width="686" height="413" src='./doc-files/marker-infowindow-classes.png' />
 *
 * @author M.Kergall
 * @see Marker
 */
public class BasicInfoWindow extends InfoWindow {

    /**
     * resource id value meaning "undefined resource id"
     */
    public static final int UNDEFINED_RES_ID = 0;

    static int mTitleId = UNDEFINED_RES_ID,
            mDescriptionId = UNDEFINED_RES_ID,
            mSubDescriptionId = UNDEFINED_RES_ID,
            mImageId = UNDEFINED_RES_ID; //resource ids

    private static void setResIds(Context context) {
//        String packageName = context.getBundleName(); //get application package name
        mTitleId = ResourceTable.Id_bubble_title; //context.getResources().getIdentifier("id/bubble_title", null, packageName);
        mDescriptionId = ResourceTable.Id_bubble_description; // context.getResources().getIdentifier("id/bubble_description", null, packageName);
        mSubDescriptionId = ResourceTable.Id_bubble_subdescription; //context.getResources().getIdentifier("id/bubble_subdescription", null, packageName);
        mImageId = ResourceTable.Id_bubble_image; //context.getResources().getIdentifier("id/bubble_image", null, packageName);

//        if (mTitleId == UNDEFINED_RES_ID || mDescriptionId == UNDEFINED_RES_ID
//                || mSubDescriptionId == UNDEFINED_RES_ID || mImageId == UNDEFINED_RES_ID) {
//            Log.e(IMapView.LOGTAG, "BasicInfoWindow: unable to get res ids in " + packageName);
//        }
    }

    public BasicInfoWindow(int layoutResId, MapView mapView) {
        super(layoutResId, mapView);

        if (mTitleId == UNDEFINED_RES_ID)
            setResIds(mapView.getContext());

        //default behavior: close it when clicking on the bubble:
        mComponent.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_UP)
                    close();
                return true;
            }
        });
    }

    @Override
    public void onOpen(Object item) {
        OverlayWithIW overlay = (OverlayWithIW) item;
        String title = overlay.getTitle();
        if (title == null)
            title = "";
        if (mComponent == null) {
            Log.w(IMapView.LOGTAG, "Error trapped, BasicInfoWindow.open, mView is null!");
            return;
        }
        Text temp = ((Text) mComponent.findComponentById(mTitleId /*R.id.title*/));

        if (temp != null) temp.setText(title);

        String snippet = overlay.getSnippet();
        if (snippet == null)
            snippet = "";
        String snippetHtml = snippet; //Html.fromHtml(snippet);
        ((Text) mComponent.findComponentById(mDescriptionId /*R.id.description*/)).setText(snippetHtml);

        //handle sub-description, hidding or showing the text view:
        Text subDescText = (Text) mComponent.findComponentById(mSubDescriptionId);
        String subDesc = overlay.getSubDescription();
        if (subDesc != null && !("".equals(subDesc))) {
            subDescText.setText(subDesc /*Html.fromHtml(subDesc)*/);
            subDescText.setVisibility(Component.VISIBLE);
        } else {
            subDescText.setVisibility(Component.HIDE);
        }

    }

    @Override
    public void onClose() {
        //by default, do nothing
    }

}
